package cn.frh.product.controller;

import cn.frh.basic.exception.BusinessException;
import cn.frh.basic.util.AjaxResult;
import cn.frh.basic.util.LoginContext;
import cn.frh.basic.util.PageList;
import cn.frh.org.domain.Employee;
import cn.frh.org.domain.Shop;
import cn.frh.org.mapper.EmployeeMapper;
import cn.frh.pet.domain.Pet;
import cn.frh.pet.query.PetQuery;
import cn.frh.product.domain.Product;
import cn.frh.product.query.ProductQuery;
import cn.frh.product.service.IProductService;
import cn.frh.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private IProductService productService;
    @Autowired
    private EmployeeMapper employeeMapper;

    @PostMapping
    public PageList<Product> queryPage(@RequestBody ProductQuery query){
        return productService.queryPage(query);
    }

    //onsale/offsale
    @PostMapping("/onsale")
    public AjaxResult onSale(@RequestBody List<Long> ids){
        try {
            productService.onSale(ids);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("骚瑞~操作失败！");
        }
    }
    @PostMapping("/offsale")
    public AjaxResult offSale(@RequestBody List<Long> ids){
        try {
            //获取当前登录人
            productService.offSale(ids);
            //service需要返回logininfo和token
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("骚瑞~操作失败！");
        }
    }

    @PutMapping
    public AjaxResult saveProduct(@RequestBody Product product,HttpServletRequest request){
        try {
            if(product.getId()==null){
                Logininfo loginUser = LoginContext.getCurrentLogininfo(request);
                Employee employee = employeeMapper.loadByLogininfoId(loginUser.getId());
                if (employee!=null||employee.getShop_id()!=null){
                    product.setShop_id(employee.getShop_id());
                    Shop shop = new Shop();
                    shop.setId(employee.getShop_id());
                    product.setShop(shop);
                    productService.add(product);
                }
            }else {
                productService.update(product);
            }
            return AjaxResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }
    }

    @GetMapping("/{id}")
    public Product getDetail(@PathVariable("id") Long id){
        return productService.loadByIdDetail(id);
    }


    /**
     * 购买服务
     * @param id 哪个服务
     * @return
     */
    @GetMapping("/adopt/{id}")
    public AjaxResult adopt(@PathVariable("id") Long id,Logininfo logininfo){
        try {
            productService.adopt(id,logininfo);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("骚瑞~操作失败！");
        }
    }
}
